package com.example.springboot.controller;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Profit;
import com.example.springboot.service.ProfitService;

import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;

@CrossOrigin
@RestController

@RequestMapping("/profit")
public class ProfitController {
    @Autowired
    ProfitService profitService;
    
    
    @PostMapping("/add")
    public Result add(@RequestBody Profit profit){
        try{
            profitService.save(profit);
        }catch(Exception e){
            return Result.error("插入数据错误");
        }
        return Result.success();
    }
    
    @PutMapping("/update")
    public Result update(@RequestBody Profit profit){
        try{
            profitService.updateById(profit);
        }catch(Exception e){
            return Result.error("更新数据错误");
        }
        return Result.success();
    }
    
    @DeleteMapping("/delete/{period}")
    public Result delete(@PathVariable String period){
        try{
            profitService.removeById(period);
        }catch(Exception e){
            return Result.error("删除数据错误");
        }
        return Result.success();
    }
    
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Profit> profitList=profitService.list();
        return Result.success(profitList);
    }
    
    @GetMapping("/selectByPeriod/{period}")
    public Result selectByPeriod(@PathVariable String period){
        Profit profit=profitService.getById(period);
        return Result.success(profit);
    }
    @GetMapping("/selectByPeriod1/{period}")
    public Result selectByPeriod1(@PathVariable String period){
        List<Profit> profitList1=profitService.selectByPeriod1(period);
        return Result.success(profitList1);
    }
    @GetMapping("/export")
        public void exportData(@RequestParam(required = false) String period,
                           @RequestParam(required = false) String periods,
                           HttpServletResponse response) throws IOException{
        ExcelWriter writer =ExcelUtil.getWriter(true);
        List<Profit> list;
        QueryWrapper<Profit> queryWrapper = new QueryWrapper<Profit>();
        if(StrUtil.isNotBlank(periods)){
            List<String> periodsArr=Arrays.stream(periods.split(",")).collect(Collectors.toList());
            queryWrapper.in("period",periodsArr);
        }else{
            //第一种全部导出或者条件导出
            queryWrapper.like(StrUtil.isNotBlank(period), "period", period);

        }
        list=profitService.list(queryWrapper);
        writer.write(list,true);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("利润信息表", "UTF-8") + ".xlsx");
        ServletOutputStream outputStream=response.getOutputStream();
        writer.flush(outputStream,true);
        writer.close();
        outputStream.flush();
        outputStream.close();
    }
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                            @RequestParam Integer pageSize,
                            @RequestParam String period) {
        QueryWrapper<Profit> queryWrapper = new QueryWrapper<Profit>();
        queryWrapper.like(StrUtil.isNotBlank(period), "period", period);
        Page<Profit> page = profitService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }
}
